package com.example.sandabackend.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.sandabackend.entity.User;
import com.example.sandabackend.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sanda/user")
public class UserController {

    @Autowired
    private UserService userService;


    @GetMapping(value = "/findall")
    public List<User> findAll(){
        return userService.findAll();
    }

    //增加和修改
    @PostMapping(value = "/save")
    public boolean save(@RequestBody User user){
//       return userService.save(user);
        return userService.saveUser(user);
    }

    //删除
    @GetMapping(value="/del/{id}")
    public Integer Delect(@PathVariable Integer id){
        return userService.delectById(id);
    }

    @GetMapping(value="/page")
    public Map findPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize,
                        @RequestParam(required = false) String username,
                        @RequestParam(required = false) String email,
                        @RequestParam(required = false) String address){
        return userService.findPage(pageNum,pageSize,username,email,address);
    }
    //mybatis-plus 姓名筛选
    @GetMapping(value="/pageM")
    public IPage<User> findPageM(@RequestParam Integer pageNum, @RequestParam Integer pageSize,
                                 @RequestParam(required = false,defaultValue="") String username,
                                 @RequestParam(required = false,defaultValue="") String email,
                                 @RequestParam(required = false,defaultValue="") String address){
        return userService.findPageByCondition(pageNum,pageSize,username,email,address);
    }

}
